﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Text.RegularExpressions;

namespace UICaltureAssist
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List<languageItems> listLang = new List<languageItems>();
        DataTable dtExcel = new DataTable();
        private void btnExport_Click(object sender, EventArgs e)
        {
            System.IO.DirectoryInfo dinfo = new System.IO.DirectoryInfo(txtPath.Text.Trim());

            
            List<singleLang> listSingle = new List<singleLang>();
            foreach (var f in dinfo.GetFiles().GroupBy(m=> getRawPage(m.Name)))
            {
                foreach (var f1 in f)
                {
                    string tag = getTag(f1.FullName);
                    getLangugae(f.Key,tag, f1.FullName, listSingle);
                }
            }

            ///行列转换.
            listLang = new List<languageItems>();
            foreach (var s in listSingle)
            {
                languageItems currLang=new languageItems();
                if (!listLang.Exists(m => m.page == s.page && m.Label == s.Label))
                {
                    listLang.Add(currLang);
                    currLang.page=s.page;
                    currLang.Label=s.Label;
                }
                else
                {
                    currLang = listLang.Single(m => m.page == s.page && m.Label == s.Label);
                }

                switch(s.Tag)
                {
                    case "en-us":
                        currLang.English=s.Language;
                        break;
                    case "zh-cn":
                        currLang.Simple=s.Language;
                        break;
                    case "zh-tw":
                        currLang.Traditional=s.Language;
                        break;
                }

            }

            dataGridView1.DataSource = listLang;
            exportToExcel();
        }

        private string getRawPage(string fileName)
        {
            return fileName.ToLower().Replace(".resx","") .Replace(".en-us","").Replace(".zh-cn","").Replace(".zh-tw","");
        }

        public void getLangugae(string page,string tag, string filename, List<singleLang> listSingle)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(filename);
            Dictionary<string, string> dicLang = new Dictionary<string, string>();
            foreach (XmlNode node in xml.SelectNodes("root/data"))
            {
                string name = node.Attributes["name"].Value;
                string value = node.SelectSingleNode("value").InnerText;
                listSingle.Add(new singleLang { page = page, Label = name, Tag=tag , Language = value });
            }
        }

        public string getTag(string fileName)
        {
            Regex reg = new Regex("aspx.([a-z-]*).resx");
            string tag= reg.Match(fileName).Groups[1].Value;
            if (tag == "")
                tag = "zh-tw";
            return tag;
        }

        private void exportToExcel()
        {

            object[,] data = new object[listLang.Count+1, 5];

            data[0, 0] = "页面";
            data[0, 1] = "标签";
            data[0, 2] = "简体";
            data[0, 3] = "繁体";
            data[0, 4] = "英文";

            for (int i = 0; i < listLang.Count; i++)
            {
                data[i + 1, 0] = listLang[i].page;
                data[i + 1, 1] = listLang[i].Label;
                data[i + 1, 2] = listLang[i].Simple;
                data[i + 1, 3] = listLang[i].Traditional;
                data[i + 1, 4] = listLang[i].English;
            }

            string excelFileName = txtExcelFile.Text.Trim();
            OisLibrary.ExcelOpt.TableToExcel(data,excelFileName , 5);
            MessageBox.Show("导出完成!");
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            string excelFileName = txtExcelFile.Text.Trim();
            dtExcel = OisLibrary.ExcelOpt.getTableFromExcel(excelFileName);

            System.IO.DirectoryInfo dinfo = new System.IO.DirectoryInfo(txtPath.Text.Trim());
            
            List<singleLang> listSingle = new List<singleLang>();
            foreach (var f in dinfo.GetFiles())
            {
                XmlDocument xmlDoc=new XmlDocument();
                xmlDoc.Load(f.FullName);

                foreach (XmlNode node in xmlDoc.SelectNodes("root/data"))
                {
                    string label = node.Attributes["name"].Value;
                    string page = getRawPage(f.Name);
                    string tag = getTag(f.Name);

                    string value = getLangugae(page, label, tag);
                    node.SelectSingleNode("value").InnerText = value;
                }

                xmlDoc.Save(f.FullName);

            }

            MessageBox.Show("导入完成!");


        }

        private string getLangugae(string page, string label, string tag)
        {
            foreach (DataRow dr in dtExcel.Rows)
            {
                if (dr[0].ToString().ToLower() == page.ToLower() &&
                    dr[1].ToString().ToLower() == label.ToLower())
                {
                    switch (tag.ToLower())
                    {
                        case "zh-cn":
                            return dr[2].ToString();
                            break;
                        case "zh-tw":
                            return dr[3].ToString();
                            break;
                        case "en-us":
                            return dr[4].ToString();
                            break;
                    }
                }
            }
            return "";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
 

    }

    public class languageItems
    {
        public string page { set; get; }
        public string Label { set; get; }
        public string Simple { set; get; }
        public string Traditional { set; get; }
        public string English { set; get; }
    }
    public class singleLang
    {
        public string page { set; get; }
        public string Label { set; get; }
        public string Tag { set; get; }
        public string Language { set; get; }
    }


}
